<mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
		   xmlns:s="library://ns.adobe.com/flex/spark" 
		   xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="400" height="344" 
		   xmlns:core="org.flexqueries2.core.*" xmlns:fields="org.flexqueries2.fields.*" xmlns:buttons="org.flexqueries2.components.buttons.*"
		   creationComplete="module1_creationCompleteHandler(event)" xmlns:datagrids="org.flexqueries2.datagrids.*">
	
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			import org.flexqueries2.components.ModuleWindow;
			import org.flexqueries2.components.buttons.ConfirmEvent;
			import org.flexqueries2.core.events.SqlEvent;
			
			/**
			 * Initialization
			 */
			protected function module1_creationCompleteHandler(event:FlexEvent):void
			{
				reload();
			}
			
			/**
			 * Close this window
			 */
			protected function btnClose_clickHandler(event:MouseEvent):void
			{
				ModuleWindow.closeWindowFor(this);
			}
			
			/**
			 * Refresh the datagrid by reloading the SqlQuery
			 */
			protected function reload(e:Event = null):void
			{
				qLoad.selectAsync();
			}
			
			/**
			 * Opens the window to create a row
			 */
			protected function btnNew_clickHandler(event:MouseEvent):void
			{
				//TODO : adjust the module name
				ModuleWindow.open('New', 'modules/the_creation_module.swf', null, reload);
			}

			/**
			 * Loads the row for edition
			 */
			protected function btnEdit_clickHandler(event:MouseEvent):void
			{
				//TODO : adjust the module name and field ID
				ModuleWindow.open('Edit', 'modules/the_creation_module.swf', grid.selectedItem.THE_ID, reload);
			}
			
			/**
			 * After deletion
			 */
			protected function qDelete_executeCompleteHandler(event:SqlEvent):void
			{
				reload();
			}

			/**
			 * Deletes the selected row
			 */
			protected function btnDelete_confirmHandler(event:ConfirmEvent):void
			{
				//TODO : adjust the ID field
				qDelete.sqlParams = [grid.selectedItem.THE_ID];
				qDelete.execAsync();
			}
			
			protected function grid_doubleClickHandler(event:MouseEvent):void
			{
				btnEdit_clickHandler(null);
			}

		]]>
	</fx:Script>
	
	<fx:Declarations>
		
		<!-- SqlQueries (load, delete) -->
		<core:SqlQuery id="qLoad" autoReadFirstRow="true" sql="get_{$table}"/>
		<!-- add this to queries.xml : <query id="get_{$table}">{$select}</query> -->
		
		<core:SqlQuery id="qDelete" sql="delete_{$table}" executeComplete="qDelete_executeCompleteHandler(event)"/>
		<!-- add this to queries.xml : <query id="delete_{$table}">{$delete}</query> -->
		
	</fx:Declarations>
	<s:HGroup height="25" bottom="5" verticalAlign="middle" textAlign="left" horizontalAlign="right" right="5" left="5">
		<buttons:ButtonNew id="btnNew" click="btnNew_clickHandler(event)"/>
		<buttons:ButtonEdit id="btnEdit" enabled="{ grid.selectedItem != null }" click="btnEdit_clickHandler(event)"/>
		<buttons:ButtonDelete id="btnDelete" enabled="{ grid.selectedItem != null }" confirm="btnDelete_confirmHandler(event)"/>
		<buttons:ButtonClose id="btnClose" click="btnClose_clickHandler(event)"/>
	</s:HGroup>
	<datagrids:SqlDataGrid id="grid" query="{ qLoad }" doubleClickEnabled="true" doubleClick="grid_doubleClickHandler(event)" right="5" left="5" top="5" bottom="35">
		<datagrids:columns>
		
			{foreach $cols col}<mx:DataGridColumn headerText="{$col}" dataField="{$col}"/>
			{/foreach}
		</datagrids:columns>
	</datagrids:SqlDataGrid>
</mx:Module>